package com.blog.controller;

import com.blog.entity.View;
import com.blog.service.BlogService;
import com.blog.service.ViewService;
import com.blog.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author suoyue_zhan
 * @date 2021/02/06 21:55
 * @descript
 */
@Api(tags = "数据统计界面显示控制器")
@RestController
public class EChartsController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private ViewService viewService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("数据统计页面调整")
    @GetMapping("/echarts")
    public ModelAndView eCharts(ModelAndView modelAndView){

        int blogTotal = blogService.getBlogTotal();
        int blogViews = blogService.getBlogViewTotal();
        int blogComments = blogService.getBlogCommentTotal();
        int blogMessages = blogService.getBlogMessageTotal();


        //redis读取文章日访问增量
        int viewTotal = 0;
        ValueOperations<String,Integer> operationNum = redisTemplate.opsForValue();
        Set<String> keys = redisTemplate.keys("blog_num:*");
        for(String key : keys){
            viewTotal += operationNum.get(key);
        }

        //LinkedHashMap：按加入时的顺序遍历
        Map<String,Integer> map = new LinkedHashMap<>();
        map.put("文章数",blogTotal);
        map.put("访问量",blogViews+viewTotal);
        map.put("评论数",blogComments);
        map.put("留言量",blogMessages);
        map.put("日增量",viewTotal);

//        //数据
//        //实时添加今日新增的访问量
//        viewList.add(new View(new Date(),viewTotal));

        modelAndView.addObject("blogTotalMap",map);
//        modelAndView.addObject("viewEcharts",viewList);
        modelAndView.setViewName("echarts.html");
        return modelAndView;
    }

    @ApiOperation("线性图表数据")
    @GetMapping("/echarts/data")
    public ResultUtil eChartsData(){

        ResultUtil result = new ResultUtil();

        long timeMillis = System.currentTimeMillis();
        Date date = new Date(timeMillis);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String endTime = simpleDateFormat.format(date);
        timeMillis -= 6*24*60*60*1000;
        date = new Date(timeMillis);
        String startTime = simpleDateFormat.format(date);

        List<View> viewList = viewService.selectViewsByDate(startTime,endTime);
        //redis读取文章日访问增量
        int viewTotal = 0;
        ValueOperations<String,Integer> operationNum = redisTemplate.opsForValue();
        Set<String> keys = redisTemplate.keys("blog_num:*");
        for(String key : keys){
            viewTotal += operationNum.get(key);
        }

        //数据
        //实时添加今日新增的访问量
        viewList.add(new View(new Date(),viewTotal));

        return result.success(viewList);
    }


    public static void main(String[] args) {
        long timeMillis = System.currentTimeMillis();
        Date date = new Date(timeMillis);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String endTime = simpleDateFormat.format(date);
        timeMillis -= 6*24*60*60*1000;
        date = new Date(timeMillis);
        String startTime = simpleDateFormat.format(date);

        System.out.println(date);
        System.out.println(endTime);
        System.out.println(startTime);

    }

}
